package com.yuntian.Vo.admin;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class AdminLoginVo implements UserDetails, Serializable   {

    private Long adminId;
    private String adminName;
    private String adminPass;
    private String role; // 新增角色字段：SUPER或NORMAL
    private String token;

    // 权限列表
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> authorities = new ArrayList<>();

        if ("SUPER".equals(this.role)) {
            // 超级管理员拥有所有权限
            authorities.add(new SimpleGrantedAuthority("ROLE_SUPER"));
            authorities.add(new SimpleGrantedAuthority("USER_READ"));
            authorities.add(new SimpleGrantedAuthority("USER_WRITE"));
            authorities.add(new SimpleGrantedAuthority("USER_DELETE"));
        } else if ("NORMAL".equals(this.role)) {
            // 普通管理员只有查询权限
            authorities.add(new SimpleGrantedAuthority("ROLE_NORMAL"));
            authorities.add(new SimpleGrantedAuthority("USER_READ"));
        }

        return authorities;
    }
    // 密码
    @Override
    public String getPassword() {
        return this.adminPass;
    }
    // 用户名
    @Override
    public String getUsername() {
        return this.adminName;
    }
    // 账号是否过期
    @Override
    public boolean isAccountNonExpired() {
        return UserDetails.super.isAccountNonExpired();
    }
    // 账号是否锁定
    @Override
    public boolean isAccountNonLocked() {
        return UserDetails.super.isAccountNonLocked();
    }
    // 密码是否过期
    @Override
    public boolean isCredentialsNonExpired() {
        return UserDetails.super.isCredentialsNonExpired();
    }
    // 账号是否启用
    @Override
    public boolean isEnabled() {
        return UserDetails.super.isEnabled();
    }
}
